package com.glorysoft.knowlespda.ui.home.viewmodel

import android.app.Activity
import androidx.databinding.ObservableArrayList
import com.glorysoft.knowlespda.BR
import com.glorysoft.knowlespda.R
import com.glorysoft.knowlespda.base.BaseViewModel
import com.glorysoft.knowlespda.base.Constant
import com.glorysoft.knowlespda.entity.BoardIdEntity
import com.glorysoft.knowlespda.entity.HomeEntity
import com.glorysoft.knowlespda.entity.ui.CallObserveBean
import com.glorysoft.knowlespda.model.HomeRepository
import com.glorysoft.knowlespda.model.LoginRepository
import com.glorysoft.knowlespda.utils.MyUtils
import com.google.gson.Gson
import com.google.gson.JsonObject
import com.google.gson.reflect.TypeToken
import com.pda.platform.ui.ui_pdaplatform.entity.FreeUI_TabIndexGridEntity
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_StringUtils
import me.tatarka.bindingcollectionadapter2.ItemBinding
import java.io.InputStream

/**
 * FileName: HomeViewModel
 * Author: XJH
 * Date: 2021/7/12 17:20
 * Description:
 * History:
 */
class HomeViewModel: BaseViewModel() {

    val regionList = arrayListOf("Replace Incoming","Replace Outgoing","Transfer","All")

    var pageNo = 1

    var homeRepository = HomeRepository()

    var homeData = HomeEntity()

    private val loginRepository by lazy { LoginRepository() }

    val onItemClick = object : OnNotClick{
        override fun onNotItemClick(item: HomeEntity.NoticeEntity) {
            defUI.callObserve.value = CallObserveBean(0,item)
        }

    }

    var noticeItems = ObservableArrayList<HomeEntity.NoticeEntity>()
    var noticeBinding =
        ItemBinding.of<HomeEntity.NoticeEntity>(BR.noticeItem, R.layout.item_notice).bindExtra(BR.notClick,onItemClick)

    fun getNotification(s : String){
        val map = JsonObject()
        map.addProperty("OPID",Constant.CURRENT_USER)
        map.addProperty("PDANO",Constant.PDA_NO)
        map.addProperty("NOTICETYPE",s)
        map.addProperty("PAGENO",pageNo)
        map.addProperty("PAGESIZE",20)

        launchOnlyResult({
            homeData = homeRepository.getNotification(map)
            if (pageNo == 1){
                noticeItems.clear()
            }
            noticeItems.addAll(homeData.noticelist)
            defUI.callObserve.value = CallObserveBean(1,0)
        })
    }

    fun loginOut(){
        val map = JsonObject()
        map.addProperty("ACTIONTYPE", "LOGOUT")
        map.addProperty("OPID", Constant.CURRENT_USER)
        launchOnlyResult({
            loginRepository.getLogin(map)
            defUI.startActivity.call()
        })
    }


    interface OnNotClick{
        fun onNotItemClick(item : HomeEntity.NoticeEntity)
    }
}